<?php
// + -----------------------------------------------------------------------------------
// | 链接：https://www.kancloud.cn/cleverstone/ymb2 
// + -----------------------------------------------------------------------------------
// | 版权：Copyright (c) 2020 Yii Manager Software LLC
// +------------------------------------------------------------------------------------
// | 日期：2024/8/11 
// + -----------------------------------------------------------------------------------
// | 作者：clevstn
// +------------------------------------------------------------------------------------

namespace builder\form\util\control;

use builder\helper\Url;

/**
 * 选择表格
 * @author clevstn
 * @since ym2.0
 */
class SelectTable extends Control
{
    private $_options = [
        'control' => 'select_table',
        'label' => '',
        'placeholder' => '请选择',
        'default' => [],
        'required' => false,
        'comment' => '',
        'help' => '',
        'layout' => 12,
        'style' => '',
        'class' => '',
        'attribute' => '',

        'nameField' => 'name', //标签字段
        'valueField' => 'id',  //值 字段
        'route' => '',         //表格路由
        'dynamicParams' => [], //动态参数（表单其他项作为访问参数。）
        'callId' => null,      //调用ID
        'width' => '920px',    //指定modal的宽;
        'height' => '620px',   //指定modal的高;
        'title' => ' ',        //指定modal标题
        'titleCloseBtn' => 1,//是否显示标题栏关闭按钮，默认显示
        'titleMaxminBtn' => false,//是否允许全屏最小化，默认否
        'refererWindow' => 'self',//模态框`iframe`的来源窗口，默认当前窗口。
        'repeat' => false,     //是否允许重复
        'tipOnRepeat' => false,//存在重复时，是否弹出提示框
        'closeOnSelected' => false,     //选择后是否关闭弹窗
    ];

    /**
     * @param string $context 模态框`iframe`的来源窗口
     * - self
     * - parent
     * - parent.parent.***
     * @return $this
     */
    public function refererWindow($context)
    {
        $this->_options['refererWindow'] = $context;
        return $this;
    }

    /**
     * 显示标题栏全屏最小化按钮
     * @return $this
     */
    public function titleMaxminBtnDisplay()
    {
        $this->_options['titleMaxminBtn'] = true;
        return $this;
    }

    /**
     * 隐藏标题栏关闭按钮
     * @return $this
     */
    public function titleCloseBtnHidden()
    {
        $this->_options['titleCloseBtn'] = 0;
        return $this;
    }

    /**
     * 控件标签
     * @param string|null $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = $label;
        return $this;
    }

    /**
     * 站位文本
     * @param string $placeholder
     * @return $this
     */
    public function placeholder($placeholder)
    {
        $this->_options['placeholder'] = (string)$placeholder;
        return $this;
    }

    /**
     * 默认值
     *
     * Here is example：
     *
     * ```Usage
     * //id: 由方法`valueField()`设置
     * //name: 由方法`nameField()`设置
     * $control->defaultValue([
     *    ['id' => 1, 'name' => 'label1']
     * ])
     * ```
     * @param array $default 默认值
     * @return $this
     */
    public function defaultValue(array $default)
    {
        $this->_options['default'] = $default;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help($help)
    {
        $this->_options['help'] = (string)$help;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout($layout)
    {
        $this->_options['layout'] = (int)$layout;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * 显示文本用的字段名
     * @param string $field 字段名，默认：name
     * @return $this
     */
    public function nameField($field)
    {
        $this->_options['nameField'] = (string)$field;
        return $this;
    }

    /**
     * 索引（值）所用字段
     * @param string $field 字段名，默认：id
     * @return $this
     */
    public function valueField($field)
    {
        $this->_options['valueField'] = (string)$field;
        return $this;
    }

    /**
     * 表格路由
     * @param string|array $route
     * @return $this
     */
    public function tableRoute($route)
    {
        $this->_options['route'] = Url::to($route, '');
        return $this;
    }

    /**
     * 表格路由动态参数（表单其他项作为路由参数。）
     * @param array $paramMaps
     * ```example
     * [
     *     'level' => 'default.user_id',
     *     'category' => 'default.type',
     * ]
     *
     * ```
     *
     * @return $this
     */
    public function dynamicParams(array $paramMaps)
    {
        $this->_options['dynamicParams'] = $paramMaps;
        return $this;
    }

    /**
     * 如果表格是由ym构建器构建，则callId为表格构建器实例时定义的ID：
     *
     * example code:
     *
     * ```code
     * // 表格
     * TableBuilder::instance(['id' => 'User_Defined_Table_Id'])
     * // 表单控件
     * $formControl->callId('User_Defined_Table_Id')
     *
     * ```
     * @param string $callId 调用ID
     * @return $this
     */
    public function callId($callId)
    {
        $this->_options['callId'] = $callId;
        return $this;
    }

    /**
     * @param string $title modal标题
     * ```php
     * // 文本标题
     * $this->title('编辑');
     *
     * // 支持HTML
     * $this->title(H5::text('编辑'));
     *
     * ```
     *
     * @return $this
     */
    public function modalTitle($title)
    {
        $this->_options['title'] = (string)$title;
        return $this;
    }

    /**
     * @param string $width 模态框的宽, 当type为modal时有效;
     * @return $this
     */
    public function width($width = '100%')
    {
        if (is_numeric($width)) {
            $width .= 'px';
        }

        $this->_options['width'] = $width;
        return $this;
    }

    /**
     * @param string $height 模态框的高, 当type为modal时有效;
     * @return $this
     */
    public function height($height = '100%')
    {
        if (is_numeric($height)) {
            $height .= 'px';
        }

        $this->_options['height'] = $height;
        return $this;
    }

    /**
     * 重复选择
     * @return $this
     */
    public function repeat()
    {
        $this->_options['repeat'] = true;
        return $this;
    }

    /**
     * 重复选择时，是否弹出提示框。
     * @return $this
     */
    public function tipOnRepeat()
    {
        $this->_options['tipOnRepeat'] = true;
        return $this;
    }

    /**
     * 选择后是否关闭弹窗
     * @return $this
     */
    public function closeOnSelected()
    {
        $this->_options['closeOnSelected'] = true;
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function _toArray()
    {
        return $this->_options;
    }
}